<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: OfficeOpenXML</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">OfficeOpenXML</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../files/app/models/office_open_xml_rb.html">
                app/models/office_open_xml.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                Object
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000011">compress</a>&nbsp;&nbsp;
      <a href="#M000009">get_from_tempate</a>&nbsp;&nbsp;
      <a href="#M000007">new</a>&nbsp;&nbsp;
      <a href="#M000010">new_xml</a>&nbsp;&nbsp;
      <a href="#M000012">stylesheet_doc</a>&nbsp;&nbsp;
      <a href="#M000008">translate</a>&nbsp;&nbsp;
      <a href="#M000006">translate</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000007" class="method-detail">
        <a name="M000007"></a>

        <div class="method-heading">
          <a href="#M000007" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(xslt, template, xml, newdoc)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000007-source">
<pre>
    <span class="ruby-comment cmt"># File app/models/office_open_xml.rb, line 11</span>
11:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">xslt</span>, <span class="ruby-identifier">template</span>, <span class="ruby-identifier">xml</span>, <span class="ruby-identifier">newdoc</span>)
12:     <span class="ruby-comment cmt">#Store the instance variables</span>
13:     <span class="ruby-ivar">@xslt</span>, <span class="ruby-ivar">@template</span>, <span class="ruby-ivar">@xml</span>, <span class="ruby-ivar">@newdoc</span> = <span class="ruby-identifier">xslt</span>, <span class="ruby-identifier">template</span>, <span class="ruby-identifier">xml</span>, <span class="ruby-identifier">newdoc</span>
14:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000006" class="method-detail">
        <a name="M000006"></a>

        <div class="method-heading">
          <a href="#M000006" class="method-signature">
          <span class="method-name">translate</span><span class="method-args">(xslt, template, xml, newdoc)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000006-source">
<pre>
   <span class="ruby-comment cmt"># File app/models/office_open_xml.rb, line 7</span>
7:   <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-identifier">xslt</span>, <span class="ruby-identifier">template</span>, <span class="ruby-identifier">xml</span>, <span class="ruby-identifier">newdoc</span>)
8:     <span class="ruby-identifier">new</span>(<span class="ruby-identifier">xslt</span>, <span class="ruby-identifier">template</span>, <span class="ruby-identifier">xml</span>, <span class="ruby-identifier">newdoc</span>).<span class="ruby-identifier">translate</span>
9:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000011" class="method-detail">
        <a name="M000011"></a>

        <div class="method-heading">
          <a href="#M000011" class="method-signature">
          <span class="method-name">compress</span><span class="method-args">(newXML)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000011-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000011-source">
<pre>
    <span class="ruby-comment cmt"># File app/models/office_open_xml.rb, line 45</span>
45:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">compress</span>(<span class="ruby-identifier">newXML</span>)
46:     <span class="ruby-comment cmt">#Copy the template to the new document</span>
47:     <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">copy</span>(<span class="ruby-ivar">@template</span>, <span class="ruby-ivar">@newdoc</span>)
48:     <span class="ruby-comment cmt">#Open the zip archive</span>
49:     <span class="ruby-constant">Zip</span><span class="ruby-operator">::</span><span class="ruby-constant">Archive</span>.<span class="ruby-identifier">open</span>(<span class="ruby-ivar">@newdoc</span>, <span class="ruby-constant">Zip</span><span class="ruby-operator">::</span><span class="ruby-constant">CREATE</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">zipfile</span><span class="ruby-operator">|</span>
50:       <span class="ruby-comment cmt">#Replace the document.xml with our new xml</span>
51:       <span class="ruby-identifier">zipfile</span>.<span class="ruby-identifier">add_or_replace_buffer</span>(<span class="ruby-value str">'word/document.xml'</span>, <span class="ruby-identifier">newXML</span>.<span class="ruby-identifier">to_s</span>)
52:     <span class="ruby-keyword kw">end</span>
53:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000009" class="method-detail">
        <a name="M000009"></a>

        <div class="method-heading">
          <a href="#M000009" class="method-signature">
          <span class="method-name">get_from_tempate</span><span class="method-args">(filename)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000009-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000009-source">
<pre>
    <span class="ruby-comment cmt"># File app/models/office_open_xml.rb, line 31</span>
31:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_from_tempate</span>(<span class="ruby-identifier">filename</span>)
32:     <span class="ruby-comment cmt">#retrieve the document from the template doc</span>
33:     <span class="ruby-identifier">xml</span> = <span class="ruby-constant">Zip</span><span class="ruby-operator">::</span><span class="ruby-constant">Archive</span>.<span class="ruby-identifier">open</span>(<span class="ruby-ivar">@template</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">zipfile</span><span class="ruby-operator">|</span>
34:       <span class="ruby-identifier">zipfile</span>.<span class="ruby-identifier">fopen</span>(<span class="ruby-identifier">filename</span>).<span class="ruby-identifier">read</span>
35:     <span class="ruby-keyword kw">end</span>
36:     <span class="ruby-comment cmt">#parse the resulting file into the Nokogiri xml doc</span>
37:     <span class="ruby-constant">Nokogiri</span><span class="ruby-operator">::</span><span class="ruby-constant">XML</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">xml</span>)
38:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000010" class="method-detail">
        <a name="M000010"></a>

        <div class="method-heading">
          <a href="#M000010" class="method-signature">
          <span class="method-name">new_xml</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000010-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000010-source">
<pre>
    <span class="ruby-comment cmt"># File app/models/office_open_xml.rb, line 40</span>
40:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">new_xml</span>
41:     <span class="ruby-comment cmt">#transform the xml values to fit out word document.</span>
42:     <span class="ruby-identifier">stylesheet_doc</span>.<span class="ruby-identifier">transform</span>(<span class="ruby-constant">Nokogiri</span><span class="ruby-operator">::</span><span class="ruby-constant">XML</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-ivar">@xml</span>)))
43:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000012" class="method-detail">
        <a name="M000012"></a>

        <div class="method-heading">
          <a href="#M000012" class="method-signature">
          <span class="method-name">stylesheet_doc</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000012-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000012-source">
<pre>
    <span class="ruby-comment cmt"># File app/models/office_open_xml.rb, line 55</span>
55:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">stylesheet_doc</span>
56:     <span class="ruby-comment cmt">#Parse the xslt into the Nokogiri XSLT</span>
57:     <span class="ruby-constant">Nokogiri</span><span class="ruby-operator">::</span><span class="ruby-constant">XSLT</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-ivar">@xslt</span>))
58:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000008" class="method-detail">
        <a name="M000008"></a>

        <div class="method-heading">
          <a href="#M000008" class="method-signature">
          <span class="method-name">translate</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000008-source">
<pre>
    <span class="ruby-comment cmt"># File app/models/office_open_xml.rb, line 16</span>
16:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">translate</span>
17:     <span class="ruby-comment cmt">#get the existing document</span>
18:     <span class="ruby-identifier">existing_xml</span> = <span class="ruby-identifier">get_from_tempate</span>(<span class="ruby-value str">&quot;word/document.xml&quot;</span>)
19:     <span class="ruby-comment cmt">#find the body node</span>
20:     <span class="ruby-identifier">body_node</span> = <span class="ruby-identifier">existing_xml</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">xpath</span>(<span class="ruby-value str">&quot;w:body&quot;</span>, {<span class="ruby-value str">&quot;w&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;http://schemas.openxmlformats.org/wordprocessingml/2006/main&quot;</span>}).<span class="ruby-identifier">first</span>
21:     <span class="ruby-comment cmt">#remove all current nodes on the body (ie clear doc)</span>
22:     <span class="ruby-identifier">body_node</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">unlink</span>
23:     <span class="ruby-comment cmt">#For each node on the new xml add it to the current xml.</span>
24:     <span class="ruby-identifier">new_xml</span>.<span class="ruby-identifier">xpath</span>(<span class="ruby-value str">&quot;*/w:body&quot;</span>, {<span class="ruby-value str">&quot;w&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;http://schemas.openxmlformats.org/wordprocessingml/2006/main&quot;</span>}).<span class="ruby-identifier">first</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">child</span><span class="ruby-operator">|</span>
25:       <span class="ruby-identifier">body_node</span>.<span class="ruby-identifier">add_child</span>(<span class="ruby-identifier">child</span>)
26:     <span class="ruby-keyword kw">end</span>
27:     <span class="ruby-comment cmt">#compress the result to a docx</span>
28:     <span class="ruby-identifier">compress</span>(<span class="ruby-identifier">existing_xml</span>)
29:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>